package org.sharelife.util;

import java.awt.image.BufferedImage;

import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.patchca.color.GradientColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.filter.predefined.DiffuseRippleFilterFactory;
import org.patchca.filter.predefined.DoubleRippleFilterFactory;
import org.patchca.filter.predefined.MarbleRippleFilterFactory;
import org.patchca.filter.predefined.RippleFilterFactory;
import org.patchca.filter.predefined.WobbleRippleFilterFactory;
import org.patchca.service.Captcha;
import org.patchca.service.ConfigurableCaptchaService;

public class PatchcaCodeMaker extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public PatchcaCodeMaker() {
		super();
	}

	// 关键之处，产生图片
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException,

	IOException {
		response.setContentType("image/png");
		ValidateEntry ve = new ValidateEntry();
		HttpSession session = request.getSession();
		session.setAttribute(session.getId(), ve.getCode()); // 验证码保存在session中
		ServletOutputStream out = response.getOutputStream();
		BufferedImage img = ve.getImage();
		ImageIO.write(img, "png", out);
		out.close();
		return;
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);

	}

	protected class ValidateEntry {
		String code;
		BufferedImage image;

		ValidateEntry() {
			createEntry();
		}

		public String getCode() {
			return code;
		}

		public BufferedImage getImage() {
			return image;
		}

		private void createEntry() {
			ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
			cs.setColorFactory(new GradientColorFactory());
			switch ((int) (System.currentTimeMillis() % 6)) {
			case 0:
				cs.setFilterFactory(new CurvesRippleFilterFactory(cs
						.getColorFactory()));
				break;
			case 1:
				cs.setFilterFactory(new MarbleRippleFilterFactory());
				break;
			case 2:
				cs.setFilterFactory(new DoubleRippleFilterFactory());
				break;
			case 3:
				cs.setFilterFactory(new WobbleRippleFilterFactory());
				break;
			case 4:
				cs.setFilterFactory(new DiffuseRippleFilterFactory());
				break;
			case 5:
				cs.setFilterFactory(new RippleFilterFactory());
				break;
			}
			Captcha captcha = cs.getCaptcha();
			code = captcha.getChallenge();
			image = captcha.getImage();
		}
	}
}
